/*
 * Coefficient.cpp
 *
 *  Created on: Jul 18, 2011
 *      Author: blyth
 */

#include "Coefficient.h"

Coefficient::Coefficient() :
			referenceSize(21),
			refTimesSize(
					{ 27, 77, 196, 381, 615, 854, 1056, 1188, 1230, 1180, 1053, 859, 659, 476, 325, 211, 132, 86, 61,
							44, 42 }), RSTR2SMO(364.293686) {
	;
}

Coefficient::~Coefficient() {
	;
}

bool Coefficient::correlates(int* stream, unsigned long numSamples, double minCoeff) {
	unsigned long dataSum = 0;
	unsigned long data2Sum = 0;

	for (int i = 0; i < referenceSize; i++) {
		dataSum += stream[i];
		data2Sum += stream[i] * stream[i];
	}

	unsigned long const NSMRS = numSamples - referenceSize;
	unsigned long const RSTR2SMOTT = RSTR2SMO * minCoeff * minCoeff * 512;

	for (int i = 0; i < NSMRS; i++) {
		int* const tempStream = &stream[i];

		unsigned long DRSTRS = tempStream[0] * refTimesSize[0] + tempStream[1] * refTimesSize[1] + tempStream[2]
				* refTimesSize[2] + tempStream[3] * refTimesSize[3] + tempStream[4] * refTimesSize[4] + tempStream[5]
				* refTimesSize[5] + tempStream[6] * refTimesSize[6] + tempStream[7] * refTimesSize[7] + tempStream[8]
				* refTimesSize[8] + tempStream[9] * refTimesSize[9] + tempStream[10] * refTimesSize[10]
				+ tempStream[11] * refTimesSize[11] + tempStream[12] * refTimesSize[12] + tempStream[13]
				* refTimesSize[13] + tempStream[14] * refTimesSize[14] + tempStream[15] * refTimesSize[15]
				+ tempStream[16] * refTimesSize[16] + tempStream[17] * refTimesSize[17] + tempStream[18]
				* refTimesSize[18] + tempStream[19] * refTimesSize[19] + tempStream[20] * refTimesSize[20];

		if (DRSTRS > (dataSum << 9)) {
			unsigned long DRSTRSMDS = DRSTRS - (dataSum << 9);
			if (DRSTRSMDS * DRSTRSMDS >= RSTR2SMOTT * (referenceSize * data2Sum - dataSum * dataSum))
				return true;
		}

		dataSum += tempStream[referenceSize] - tempStream[0];
		data2Sum += tempStream[referenceSize] * tempStream[referenceSize] - tempStream[0] * tempStream[0];
	}

	return false;
}
